package com.example.pt.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.pt.dto.UserFollowDTO;
import com.example.pt.service.UserFollowService;
import com.example.pt.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户关注控制器
 */
@RestController
@RequestMapping("/api/follow")
@CrossOrigin(origins = "*")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 获取关注列表
     * @param header 请求头
     * @return 关注列表
     */
    @GetMapping("/list")
    public ResponseEntity<?> getFollowingList(@RequestHeader Map<String, String> header) {
        // 从request中获取当前用户ID
        String token =  header.get("token");
        DecodedJWT decode;
        Map<String, Object> response = new HashMap<>();

        try {
            decode = JWTUtil.verifyToken(token);
        } catch (Exception e) {

            response.put("token", "登录权限已失效！");
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }
        int userId = decode.getClaim("uid").asInt();
        
        List<UserFollowDTO> followList = userFollowService.getFollowingList(userId);
        return ResponseEntity.ok(followList);
    }
    
    /**
     * 关注用户
     * @param followedId 被关注用户ID
     * @param header 请求头
     * @return 结果
     */
    @PostMapping("/follow/{followedId}")
    public ResponseEntity<?> followUser(@PathVariable Integer followedId, @RequestHeader Map<String, String> header) {
        // 从request中获取当前用户ID
        String token =  header.get("token");
        DecodedJWT decode;
        Map<String, Object> response = new HashMap<>();
        try {
            decode = JWTUtil.verifyToken(token);
        } catch (Exception e) {

            response.put("token", "登录权限已失效！");
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }

        int userId = decode.getClaim("uid").asInt();
        

            if(userFollowService.followUser(userId, followedId))
            return ResponseEntity.ok("关注成功");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("关注失败");

    }
    
    /**
     * 取消关注
     * @param followedId 被关注用户ID
     * @param header 请求头
     * @return 结果
     */
    @PostMapping("/unfollow/{followedId}")
    public ResponseEntity<?> unfollowUser(@PathVariable Integer followedId, @RequestHeader Map<String, String> header) {
        // 从request中获取当前用户ID
        String token =  header.get("token");
        DecodedJWT decode;

        Map<String, Object> response = new HashMap<>();
        try {
            decode = JWTUtil.verifyToken(token);
        } catch (Exception e) {

            response.put("token", "登录权限已失效！");
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }

        int userId = decode.getClaim("uid").asInt();
        
        try {
            userFollowService.unfollowUser(userId, followedId);
            return ResponseEntity.ok("取消关注成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("取消关注失败" + e.getMessage());
        }
    }
    
    /**
     * 检查是否关注了指定用户
     * @param followedId 被关注用户ID
     * @param header 请求头
     * @return 是否已关注
     */
    @GetMapping("/check/{followedId}")
    public ResponseEntity<?> checkFollowStatus(@PathVariable Integer followedId, @RequestHeader Map<String, String> header) {
        // 从request中获取当前用户ID
        String token =  header.get("token");
        DecodedJWT decode;
        Map<String, Object> response = new HashMap<>();
        try {
            decode = JWTUtil.verifyToken(token);
        } catch (Exception e) {

            response.put("token", "登录权限已失效！");
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }

        int userId = decode.getClaim("uid").asInt();
        
        boolean isFollowing = userFollowService.checkFollowStatus(userId, followedId);
        return ResponseEntity.ok(isFollowing);
    }
} 